package view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class MelloImageView extends ImageView {
    private Paint paint;
    public MelloImageView(Context context) {
        super(context);
    }

    public MelloImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MelloImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
    }

    public MelloImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected void onDraw(Canvas canvas){
        Drawable drawable = getDrawable();
        if (drawable != null) {
            Bitmap bitmap = getBitmapFromDrawable(drawable);
            Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 20, 0);
            final Rect rectStr = new Rect(0, 0, b.getWidth(), b.getHeight());
            final Rect rectDest = new Rect(0, 0, getWidth(), getHeight());

            canvas.drawBitmap(b,rectStr,rectDest,paint);
        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getRoundBitmapByShader(Bitmap bitmap,int outWidth, int outHeight, int radius, int border) {
        if (bitmap == null){
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float widthScale = outWidth * 1f /width;
        float heightScale = outHeight * 1f /height;

        Matrix matrix = new Matrix();
        matrix.setScale(widthScale,heightScale);
        // 创建需要输出的bitmap
        Bitmap desBitmap = Bitmap.createBitmap(outWidth,outHeight,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(desBitmap);
        Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);

        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        bitmapShader.setLocalMatrix(matrix);
        paint1.setShader(bitmapShader);
        RectF rect = new RectF(border,border, outWidth - border, outHeight - border);
        canvas.drawRoundRect(rect,radius,radius,paint1);
        return desBitmap;
    }

    private Bitmap getBitmapFromDrawable(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width,height,drawable.getOpacity()!= PixelFormat.OPAQUE? Bitmap.Config.ARGB_8888
                :Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);

        return bitmap;
    }
}
